﻿using Messhall.Framework.Domain.DomainObject;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Messhall.Domain.Permission.BusinessObjects
{
    public class UsersEntity : EntityBoBase
    {
        #region 成员变量

        /// <summary>
        /// 用户名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 用户手机号
        /// </summary>
        public string Mobile { get; set; }

        /// <summary>
        /// 用户编号
        /// </summary>
        public string AccountNo { get; set; }

        /// <summary>
        /// 用户密码
        /// </summary>
        public string PassWord { get; set; }

        /// <summary>
        /// 账户余额
        /// </summary>
        public decimal Money { get; set; }

        /// <summary>
        /// 是否是Vip
        /// </summary>
        public bool IsVip { get; set; }

        /// <summary>
        /// 积分
        /// </summary>
        public int Integral { get; set; }

        #endregion
    }

    public class Users : BoBase, IAggregateRoot
    {
        private UsersEntity SelfEntity
        {
            get { return Entity as UsersEntity; }
            set { this.Entity = value; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="entity"></param>
        public Users(UsersEntity entity)
        {
            this.SelfEntity = entity;
        }
        
        public Users(string name, string mobile, string accountNo, string passWord, decimal money)
        {
            UsersEntity entity = new UsersEntity();
            entity.Name = name;
            entity.Mobile = mobile;
            entity.AccountNo = accountNo;
            entity.PassWord = passWord;
            entity.Money = money;
            this.SelfEntity = entity;
        }

        /// <summary>
        /// 用户角色
        /// </summary>
        public List<UsersRole> UsersRoles { get; private set; }

        /// <summary>
        /// 用户第三方登录账号
        /// </summary>
        public List<Account> Accounts { get; private set; }

        /// <summary>
        /// 获取用户名称
        /// </summary>
        /// <returns></returns>
        public string GetName() => this.SelfEntity.Name;

        /// <summary>
        /// 设置用户名称
        /// </summary>
        /// <param name="name"></param>
        public void SetName(string name)
        {
            this.SelfEntity.Name = name;
        }

        /// <summary>
        /// 获取用户编号
        /// </summary>
        /// <returns></returns>
        public string GetAccountNo() => this.SelfEntity.AccountNo;

        /// <summary>
        /// 获取用户编号
        /// </summary>
        /// <returns></returns>
        public void SetAccountNo(string accountNo)
        {
            this.SelfEntity.AccountNo = accountNo;
        }

        /// <summary>
        /// 获取用户手机号
        /// </summary>
        /// <returns></returns>
        public string GetMobile() => this.SelfEntity.Mobile;

        /// <summary>
        /// 设置用户手机号
        /// </summary>
        /// <returns></returns>
        public void SetMobile(string mobile)
        {
            this.SelfEntity.Mobile = mobile;
        }

        /// <summary>
        /// 获取用户密码
        /// </summary>
        /// <returns></returns>
        public string GetPassWord() => this.SelfEntity.PassWord;

        /// <summary>
        /// 设置用户密码
        /// </summary>
        /// <param name="passWord"></param>
        public void SetPassWord(string passWord)
        {
            if (!string.IsNullOrWhiteSpace(passWord))
            {
                this.SelfEntity.PassWord = passWord;
            }
        }

        /// <summary>
        /// 获取用户余额
        /// </summary>
        /// <returns></returns>
        public decimal GetMoney() => this.SelfEntity.Money;

        /// <summary>
        /// 设置用户余额
        /// </summary>
        /// <param name="money"></param>
        public void SetMoney(decimal money)
        {
            this.SelfEntity.Money = money;
        }

        /// <summary>
        /// 获取用户是否是会员
        /// </summary>
        /// <returns></returns>
        public bool GetVip() => this.SelfEntity.IsVip;

        /// <summary>
        /// 设置用户为VIP
        /// </summary>
        public void SetVip(bool isVip)
        {
            this.SelfEntity.IsVip = isVip;
        }

        /// <summary>
        /// 获取用户饭币
        /// </summary>
        /// <returns></returns>
        public int GetIntegral() => this.SelfEntity.Integral;


        /// <summary>
        /// 设置用户饭币
        /// </summary>
        /// <returns></returns>
        public void SetIntegral(int integral)
        {
            this.SelfEntity.Integral = integral;
        }

        /// <summary>
        /// 获取用户角色信息
        /// </summary>
        /// <returns></returns>
        public void GetRole()
        {
        }

        /// <summary>
        /// 设置用户角色信息
        /// </summary>
        /// <returns></returns>
        public void SetRole(Guid roleKeyId)
        {
            //if (this.UsersRoles.Any(role => role.RoleKeyId != roleKeyId))
            //{
            //    this.UsersRoles.Add(new(roleKeyId, this.SelfEntity.KeyId));
            //}
        }

        /// <summary>
        /// 删除用户角色信息
        /// </summary>
        /// <returns></returns>
        public void RemoveRole(Guid roleKeyId)
        {
            //if (this.UsersRoles.Any(role => role.RoleKeyId == roleKeyId))
            //{
            //    this.UsersRoles.Remove(new(roleKeyId, this.SelfEntity.KeyId));
            //}
        }

        /// <summary>
        /// 设置用户登录方式
        /// </summary>
        /// <param name="account"></param>
        public void RemoveAccount(Account account)
        {
            this.Accounts.Remove(account);
        }

        /// <summary>
        /// 设置用户登录方式
        /// </summary>
        /// <param name="account"></param>
        public void SetAccount(Account account)
        {
            this.Accounts.Add(account);
        }
    }
}